package com.lottery.htbc.thread;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lottery.htbc.dto.UserBetConfigDto;
import com.lottery.htbc.service.LuckDrawServiceImpl;
import com.lottery.htbc.utils.FastJsonUtils;
import com.lottery.htbc.utils.service.redis.RedisService;

/**
 * 急速pk10
 *
 */
@SuppressWarnings("rawtypes")
public class LuckDrawJspkeRunnable extends LuckDrawBase implements Runnable {


	private final static Logger LOGGER = LoggerFactory.getLogger(LuckDrawJspkeRunnable.class);


	public LuckDrawJspkeRunnable(RedisService redisService,
			UserBetConfigDto betConfig,LuckDrawServiceImpl luckDrawService) {
		super(redisService, betConfig,luckDrawService);
		setRoomId(46);
	}


	//投注
	public  void  bettingJspk()throws Exception{
		if(!accountAhead()){
			return ;
		}
		Map roundMap=initGame();
		if(roundMap!=null){
			LOGGER.info("账户:{},游戏标识 [ {} ]游戏环境初始化完成============== ",betConfig.getAccount(),roundMap.get("id"));
			String round_id= roundMap.get("id").toString();
			String betStrInfo=redisService.get(betConfig.getAccount()+"_"+round_id);
			if(betStrInfo==null){
				Map<String ,Object> prarm=crateBettingParam();
				prarm.put("round_id",round_id);
				redisService.set(betConfig.getAccount()+"_"+round_id, FastJsonUtils.mapToJSONString(prarm),MINUTE*REPEAT_TIME);
				betting(prarm);
			}
			else{
				LOGGER.info("账户:{},游戏标识 [ {} ]重复投注=================重复投注   ",betConfig.getAccount(),roundMap.get("id"));
				LOGGER.info("\n");
			}


		}
	}

	/**
	 * 倍数对应的下班<br/>  默认10(第11个数字)
	 */
	private  int beiIndex=10;

	/**
	 * 手动设置基础倍数
	 */
	private static final  int[] BEISHUS_5_GRADE_1={1,1,1,2,3,4,1,2,3,4,
		5,12,24,48,60,64,1,3,10,30,
		60,64,4,8,16,32,64,67,70,70};
	/**
	 * 手动设置基础倍数
	 */
	private static final  int[] BEISHUS_5_GRADE_2={1,1,1,2,3,4,3,4,5,6,
		7,20,45,100,200,210,1,3,10,30,
		60,120,4,8,16,32,64,67,70,70};

	/**
	 * 手动设置基础倍数
	 */
	private static final  int[] BEISHUS_6_GRADE_1={1,1,1,1,2,4,1,2,3,6,
		10,20,50,130,135,140,1,3,10,30,
		75,85,1,3,10,30,75,80,90,100};
	
	/**
	 * 手动设置基础倍数
	 */
	private static final  int[] BEISHUS_6_GRADE_2={1,1,1,1,2,4,1,2,3,6,
		10,30,90,270,810,810,1,3,10,30,
		75,85,1,3,10,30,75,80,90,100};

	private	int[] beishus;

	/**
	 * 倍数过期时间  48个小时
	 * 分钟
	 */
	private  static  final  int  BS_TIME=60*48;
	
	/**
	 * 模式转化单位
	 */
	private  static  final  int  MODEL_ATOM=10;

	/**
	 * 一分钟对应的秒
	 */
	protected  static  final  int  MINUTE=60;
	//构造倍数 金额条件
	private Map crateBettingParam()throws Exception{
		if(this.betConfig.getZhu()==5){
			beishus=BEISHUS_5_GRADE_1;
		}
		else if(this.betConfig.getZhu()==6){
			beishus=BEISHUS_6_GRADE_1;
		}
		String redisBsKey="HTBC_BS_INDEX_CURRENT_"+betConfig.getAccount();
		String bsStr=redisService.get(redisBsKey);
		if(bsStr!=null){
			this.beiIndex=Integer.valueOf(bsStr);
		}
		else{
			redisService.set(redisBsKey, this.beiIndex+"",MINUTE*BS_TIME);
		}
		LOGGER.info("账户:{},当前倍数下标为:{},注数为:{},对应的基础倍数为:{},加权[{}]后的倍数为:{}",betConfig.getAccount(),beiIndex,betConfig.getZhu(),beishus[beiIndex],
				betConfig.getPower(),beishus[beiIndex]*betConfig.getPower());

		Map<String ,Object> prarm=new HashMap<>();
		Map<String,Object> order=new HashMap<>();
		// 大双中的单
		//String balls=constructionValue(1,1);
		String balls=constructionRd();
		order.put("balls",balls);
		//注数
		int zhu=betConfig.getZhu();
		order.put("zhu",zhu);
		//模式
		float mode=betConfig.getMode();
		//计算倍数 基础倍数*权重
		int bei=beishus[beiIndex]*betConfig.getPower();
		//如果最终的倍数刚好是10的倍数则倍除以10 模式乘以10 最终的投注金额不变
		if(bei%MODEL_ATOM==0){
			mode=mode*MODEL_ATOM;
			bei=bei/MODEL_ATOM;
		}
		//模式 *单价 *倍数 *注数 *权重
		float money=mode*betConfig.getPrice()*bei*zhu;
		order.put("bei",bei);
		order.put("mode",mode);
		String moneyStr= String.format("%.2f", money);
		//金额
		order.put("money",moneyStr);
		order.put("type","dingwei");
		order.put("base",2);
		List<Map> orders=new ArrayList<>();
		orders.add(order);
		prarm.put("orders",orders);
		return prarm;
	}

	@Override
	public void run() {
		try {
			this.bettingJspk();
		} catch (Exception e) {
		}
	}

}
